<?php
namespace app\home\controller;

use app\common\component\help_to\HelpToModel;
use app\common\component\help_appointment\HelpAppointmentModel;
use app\common\component\help_appointment\HelpAppointmentService;
use app\common\component\help_appointment\HelpAppointmentOldModel;

class Appointment extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}
	
	public function checkFirstOrder() {
		$checkHelpTo = HelpToModel::get(['user_id' => \think\Session::get('user_id'), 'status' => 4]);
    	if(!$checkHelpTo) {
    		echo '<meta charset="utf-8"><script>alert("请先完成新手体验区体验!");window.location.href="/diggings/1";</script>';exit;
    	}
	}
	
    public function index() {
    	$this -> checkFirstOrder();

    	// 清空预约昨天的预约数据
    	(new HelpAppointmentService()) -> clearTable();

    	if($this -> request -> isPost()) {
    		$this -> create();
    	}

		$helpAppointmentService = new HelpAppointmentService();

		$resultHelpAppointment = $helpAppointmentService -> getHelpAppointment();
		if($resultHelpAppointment['code'] === 0) {
			$this -> view -> assign('help_appointment', $resultHelpAppointment['data']['help_appointment']);
		}

		$resultHelpAppointmentLastId = $helpAppointmentService -> getHelpAppointmentLastId();
		$this -> view -> assign('helpAppointmentLastId', $resultHelpAppointmentLastId['data']['help_appointment_id']);

		$this -> view -> assign('currentHelpAppointment', HelpAppointmentModel::where('to_num > 0') -> count());

		$this -> view -> assign('helpAppointments', HelpAppointmentOldModel::where('user_id', \think\Session::get('user_id')) -> order('status DESC, help_appointment_id DESC') -> paginate(10));

    	return $this -> view -> fetch();
    }

    public function create() {
    	$post = $this -> request -> post();

		if($this -> validate($post, ['appointment_money' => 'require|token']) !== true) {
			echo '<meta charset="utf-8"><script>window.location.href="/appointment";</script>';exit;
		}
		unset($post['__token__']);

		$currentTime = time();
		$APPOINTMENT_TIME_STARTS = explode('.',getParam('APPOINTMENT_TIME_START'));
		$APPOINTMENT_TIME_START_H = $APPOINTMENT_TIME_STARTS[0];
		$APPOINTMENT_TIME_START_I = isset($APPOINTMENT_TIME_STARTS[1])?$APPOINTMENT_TIME_STARTS[1]:00;
		$APPOINTMENT_TIME_START_S = strtotime($APPOINTMENT_TIME_START_H. ':' . $APPOINTMENT_TIME_START_I);

		$APPOINTMENT_TIME_ENDS = explode('.',getParam('APPOINTMENT_TIME_END'));
		$APPOINTMENT_TIME_END_H = $APPOINTMENT_TIME_ENDS[0];
		$APPOINTMENT_TIME_END_I = isset($APPOINTMENT_TIME_ENDS[1])?$APPOINTMENT_TIME_ENDS[1]:'00';
		$APPOINTMENT_TIME_END_S = strtotime($APPOINTMENT_TIME_END_H. ':' . $APPOINTMENT_TIME_END_I);

		if(($currentTime < $APPOINTMENT_TIME_START_S) || ($currentTime > $APPOINTMENT_TIME_END_S)) {
			echo '<meta charset="utf-8"><script>alert("不在' . 
				$APPOINTMENT_TIME_START_H . '点' . $APPOINTMENT_TIME_START_I .'分 ~ ' .
				$APPOINTMENT_TIME_END_H . '点' . $APPOINTMENT_TIME_END_I .
				'分的预约时间内!");window.location.href="/appointment";</script>';exit;
		}

    	// 判断是否VIP
    	if((new \app\common\logic\UserVip()) -> isVip()) {
    		if($post['appointment_money'] < getParam('APPOINTMENT_VIP_MONEY_MIN')) {
    			echo '<meta charset="utf-8"><script>alert("您预约的' . $post['appointment_money'] . '金额小于指定金额!");window.history.go(-1);</script>';exit;
    		} else if($post['appointment_money'] > getParam('APPOINTMENT_VIP_MONEY_MAX')) {
    			echo '<meta charset="utf-8"><script>alert("您预约的' . $post['appointment_money'] . '金额大于指定金额!");window.history.go(-1);</script>';exit;
    		}
    	} else {
    		if($post['appointment_money'] < getParam('APPOINTMENT_MONEY_MIN')) {
    			echo '<meta charset="utf-8"><script>alert("您预约的' . $post['appointment_money'] . '金额小于指定金额!");window.history.go(-1);</script>';exit;
    		} else if($post['appointment_money'] > getParam('APPOINTMENT_MONEY_MAX')) {
    			echo '<meta charset="utf-8"><script>alert("您预约的' . $post['appointment_money'] . '金额大于指定金额!");window.history.go(-1);</script>';exit;
    		}
    	}

		$resultHelpAppointment = (new HelpAppointmentService()) -> getHelpAppointment();
		if($resultHelpAppointment['code'] === 0) {
			echo '<meta charset="utf-8"><script>alert("您今天已经预约，请耐心等候匹配!");window.history.go(-1);</script>';exit;
		}

    	$resultHelpAppointment = (new HelpAppointmentService()) -> appointment($post['appointment_money']);
    	if($resultHelpAppointment['code'] !== 0) {
    		echo '<meta charset="utf-8"><script>alert("' . $resultHelpAppointment['message'] . '!");window.history.go(-1);</script>';exit;
    	}
		
		// 执行预约匹配
		//(new HelpAppointmentService()) -> matchHelpGetOrder();

    	echo '<meta charset="utf-8"><script>alert("' . $resultHelpAppointment['message'] . '!");window.location.href="/appointment";</script>';exit;
    	
    }
    
    public function delete() {
    	$post = $this -> request -> post();
    	
    	return json((new HelpAppointmentService()) -> delete($post['help_appointment_id']));
    }
}
?>